package org.rsbot.script.internal;

import java.util.Random;

public class BreakHandler
{
  protected final Random random = new Random();
  private long nextBreak;
  private long breakEnd;
  private int ticks = 0;

  public boolean isBreaking() {
    return (this.ticks > 50) && (this.nextBreak > 0L) && (this.nextBreak < System.currentTimeMillis()) && (this.breakEnd > System.currentTimeMillis());
  }

  public void tick()
  {
    this.ticks += 1;
    if ((this.nextBreak < 0L) || (this.nextBreak - System.currentTimeMillis() < -30000L)) {
      this.ticks = 0;
      int i = random(20, 120) * 60000;
      this.nextBreak = (System.currentTimeMillis() + i);
      if (random(0, 4) != 0)
        this.breakEnd = (this.nextBreak + random(2, 40) * 60000 + i / 6);
      else
        this.breakEnd = (this.nextBreak + random(10, 60) * 1000);
    }
  }

  public long getBreakTime()
  {
    return this.breakEnd - System.currentTimeMillis();
  }

  private int random(int paramInt1, int paramInt2) {
    int i = Math.abs(paramInt2 - paramInt1);
    return Math.min(paramInt1, paramInt2) + (i == 0 ? 0 : this.random.nextInt(i));
  }
}